<?php
/**
 * changxm   test
 * 2011-11 家教网  news
 *
 */
class NewsAction extends Action
{ 
	public function __call($name, $param){
		$name = intval($name);
		$this->show($name);
	}
    public function index()
    {
    	$m = M('News');
    	$data = $m->select();
    	$this->assign('data', $data);
        $this->display();
    }
    
    public function show($oid){
    	$m = M('News');
    	$id = 0;
    	$id = isset($_GET['id']) ? $_GET['id']: $oid;
    	
//    	$data = $m->where('id='.$id)->select();
    	$data = $m->where('id='.$id)->find($id);
    	//增一次阅读
    	$setarr = array('views'=>intval($data['views']) +1 );
//    	dump($setarr);
    	$m->where('id='.$id)->save($setarr);
    	
    	
    	$this->assign('data', $data);
    	$this->display('show');
    	
    }
    
    public function add(){
    	if($_POST){
    		$param=$_POST;
	    	$htmlData = '';
			if (!empty($param['content'])) {
				if (get_magic_quotes_gpc()) {
					$htmlData = stripslashes($param['content']);
				} else {
					$htmlData = $param['content'];
				}
			}
			$param['content'] = $htmlData;
    		$m = M('News');
    		if($_POST['id']){    			
    			unset($param['id']);
    			$m->where('id='.$_POST['id'])->save($param);
    			$m->save();
    		}else{		
    			unset($param[id]);
    			$m->add($param);
    		}
    		
    	}
    	if(isset($_GET['id'])){
    		$id = intval($_GET['id']);
    		if($id){
    			$m = M('News');
    			$data = $m->where('id='.$id)->find();
    			$this->assign('data', $data);
    			
    		}
    	}
    	$this->display();
    }
    
}
?>